package servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.Date;
import java.util.Scanner;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.http.Part;

import SqlJavaBean.MakeTestBean;
import SqlJavaBean.TeacherW;


/**
 * Servlet implementation class makeTest
 */
@WebServlet("/makeTest.view")
@MultipartConfig()
public class makeTest extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public makeTest() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		//response.getWriter().append("Served at: ").append(request.getContextPath());
		response.setCharacterEncoding("UTF-8");
		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=utf-8");
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=utf-8");
		Part part1 = request.getPart("subjectFile");
		Part part2 = request.getPart("testEnvironmentFile");
		String examName = request.getParameter("examName");
		String startTime = request.getParameter("startTime");
		String endTime = request.getParameter("endTime");
		String whenLong = request.getParameter("whenLong");
		String counts = request.getParameter("subjectCounts");
		PrintWriter out = response.getWriter();
		String subjectFile = readFile(part1);
		String testEnvironmentFile = readFile(part2);
		if(subjectFile.equals("fail") || testEnvironmentFile.equals("fail")) {
			out.print("fail!2222");
			return;
		}
		HttpSession s = request.getSession();
		Connection con = (Connection)s.getAttribute("con");
		if(con==null){
			out.print("您的身份已过期，请重新登录！");
			return;
		}
		MakeTestBean make = new MakeTestBean(con);
		int wl =Integer.parseInt(whenLong);
		int c =Integer.parseInt(counts);
		String f1= make.createAnExam(examName, startTime, endTime, wl, c);
		String f2 = make.addExamData(examName, subjectFile);
		String f3 = make.createEnvironment(testEnvironmentFile);
		if(!f1.equals("OK")) request.setAttribute("callback","fail('发题失败!已存在同名考试,请更改考试名称')");
		else if(!f2.equals("OK"))request.setAttribute("callback","fail('添加题目失败！请检查题目格式')");
		else if(!f3.equals("OK"))request.setAttribute("callback","fail('创建考试环境失败!请检查.SQL文件格式，必须符合MySql语法')");
		else request.setAttribute("callback","success()");
		request.getRequestDispatcher("makeTest.jsp").forward(request, response);
	}
	
	private String readFile(Part p){
		Scanner sc = null;
		try{
			sc = new Scanner(p.getInputStream());
		}
		catch(Exception e){
			return e.getMessage();
		}
		String str = "";
		while(sc.hasNext()) str = str +sc.nextLine();
		return str;
	}

}
